package com.juyu.miliao.api.controller;

import java.util.Map;

import javax.annotation.Resource;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.juyu.miliao.api.domain.dto.UserCallUpdateDTO;
import com.juyu.miliao.api.service.UserInfoService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import com.juyu.miliao.api.service.UserCallService;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/userCall")
public class UserCallController {
	@Resource
	UserCallService userCallService;
	@Resource
	UserInfoService userInfoService;

	/**
	 * 用户是否接听通话
	 *
	 * @param type   1:接通成功，2:接通失败
	 */
	@GetMapping("/callSum/{fromUserId}-{toUserId}/{type}")
	public void addUserCallSum(@PathVariable("type") Integer type,
							   @PathVariable("fromUserId") Integer fromUserId,
							   @PathVariable("toUserId") Integer toUserId) {
		if (type != 1 && type != 2)
			throw new IllegalParamException("参数错误：type不能为0");
		if (toUserId == 0 || fromUserId==0)
			throw new IllegalParamException("参数错误：type不能为0");
		userInfoService.addUserCallSum(type, fromUserId,toUserId);
	}
	
	/**
	 * 1.5.6废弃 改用chatSpendService维护
	 * 主播通话状态
	 * @param type 1开始通话  2结束通话
	 * @param userId 主播id
	 */
	@GetMapping("/{type}/{userId}")
	public void userCallInfo(@PathVariable("type") Integer type,@PathVariable("userId") Integer userId){
//		userCallService.userCallInfo(type,userId);
	}
	
	/**
	 * 随机获取在线、未通话主播userId
	 * @return
	 */
	@GetMapping
	public JSONObject getUserCall(){
		JSONObject json = new JSONObject();
		String userId = userCallService.getUserCall();
		json.put("userId", userId);
		return json;
	}
	
	/**
	 * 获取打招呼功能 主播列表(优先展示在线主播，其他使用随机主播填充)
	 * @param type 类型，1：在线，2：同城
	 * @return
	 */
	@GetMapping("/list/{type}")
	public Map<String,Object> getUserCallList(Authentication token, @PathVariable("type") int type){
		Subject subject = (Subject) token.getPrincipal();
		return userCallService.getUserCallList(subject.getId(),type);
	}

	@PostMapping("/call/number")
	public void updateCallNumber(Authentication token,UserCallUpdateDTO userCallUpdateDTO) throws JsonProcessingException {
		Subject subject= (Subject) token.getPrincipal();
		userCallService.updateCallNumber(subject.getId(),userCallUpdateDTO);
	}
}
